<? namespace Clases;

include 'arquitectura/ConexionMySQL.php';
include 'arquitectura/IJirach.php';

abstract class AbstractJIrach implements IJirach
{

    public $strInsertar,$strDelete,$strUpdate,$strInsertarId,$id,$tabla;
    protected abstract function fillJirach($row);
    protected abstract function Comandos();
    public abstract function get($object);

    function AbstractJirach()
    {
        $this->Comandos();
    }

    public function delete() {
        $conexion = new ConexionMySQL();
        $conexion->AccionesDB($this->getAtributos(),$this->strDelete);
        $conexion->close();
    }

    public function getAtributosInsertar() {
        $lst = $this->getAtributos();
        $nuevoarray = $lst;
        array_pop($nuevoarray);
        return $nuevoarray;
    }

    public function insertar() {
        $conexion = new ConexionMySQL();
        $conexion->AccionesDB($this->getAtributos(),$this->strInsertar);
        $conexion->close();
    }
    public function insertarId() {
        $conexion = new ConexionMySQL();
        $insertar = $conexion->InsertarID($this->getAtributosInsertar(), $this->strInsertar, $this->id, $this->tabla);
        $conexion->close();
        return $insertar;
    }

    public function update() {
        $conexion = new ConexionMySQL();
        $conexion->AccionesDB($this->getAtributos(),$this->strUpdate);
        $conexion->close();
    }

    public static function getAllAbstract($tabla, $id, $objeto)
    {
        $cm = new ConexionMySQL();
        $query = "SELECT * FROM ".$tabla;
        $data = $cm->GetQuery($query);
        $lst = array();
        $cm->close();
        try {
            while ($row = mysql_fetch_row($data)) {
                $abstract = AbstractJirach::getAbstractJirach($objeto,$row[0],$tabla,$id);
                array_push($lst,$abstract);
            }

        } catch (Exception $ex) {

        }
        
        return $lst;
    }
    
   public static function getAllAbstractC($tabla, $id,$oid,$objeto)
    {
        $cm = new ConexionMySQL();
        $query = "SELECT * FROM ".$tabla;
        $data = $cm->GetQuery($query);
        $lst = array();
        $cm->close();
        try {
            while ($row = mysql_fetch_row($data)) {
                $abstract = AbstractJirach::getAbstractJirachC($objeto,$row[0],$tabla,$id,$oid,$row[1]);
                array_push($lst,$abstract);
            }

        } catch (Exception $ex) {

        }
        
        return $lst;
    }

    public static function getAllFrom($id, $tabla, $Id, $Value, $objeto)
    {
        $str = "SELECT ".$id." FROM ".$tabla." WHERE ".$Id. " = " . $Value;
        $cm = new ConexionMySQL();
        $data = $cm->GetQuery($str);
        $lst = array();
        while ($row = mysql_fetch_row($data)) {
                array_push($lst,$row[0]);
        }
        $cm->close();
        return $lst;
    }

    public static function getAbstractJirach($abstract, $value, $tabla, $id) 
    {    
        try{
            $conexion = new ConexionMySQL();
            $query = "SELECT * FROM ".$tabla." where ".$id." = '" . $value . "'";
            
            $rs = $conexion->GetQuery($query);
            if($tabla == "`alumno`")
                $abstract = new Alumno();
            else if ($tabla == "`docente`")
                $abstract = new Docente();
            else if ($tabla == "`reporte`")
                $abstract = new Reporte();
            else if ($tabla == "`nombrealumno`")
                $abstract = new ReporteAlumno();
            $abstract->fillJirach($rs);
            return $abstract;
        }
        catch(Exception $e){return null;}
    }
    
    public static function getAbstractJirachC($abstract, $value, $tabla, $id,$id2,$value2) 
    {    
        try{
            $conexion = new ConexionMySQL();
            $query = "SELECT * FROM ".$tabla." where ".$id." = '" . $value . "' AND ".$id2." = ".$value2;
            $rs = $conexion->GetQuery($query);
            if($tabla == "`alumno`")
                $abstract = new Alumno();
            else if ($tabla == "`docente`")
                $abstract = new Docente();
            else if ($tabla == "`reporte`")
                $abstract = new Reporte();
            else if ($tabla == "`nombrealumno`")
                $abstract = new ReporteAlumno();
            $abstract->fillJirach($rs);
            return $abstract;
        }
        catch(Exception $e){return null;}
    }

}
?>